<?php
class FilterListForm extends Form
{
	function FilterListForm()
	{
		Form::Form('FilterListForm');
		
		CGlobal::$website_title='Thuộc tính chuyên mục';
		
		AZLib::getFilters();
		
		#############################################################################
		#Xóa filter_group
		$fgroup_del=(int)Url::get('fgroup_del',0);
		
		if($fgroup_del)
		{
			$filter_group	= DB::select('filter_group','id='.$fgroup_del);
			
			if($filter_group)
			{
				DB::delete("filter_group",	"id=$fgroup_del");
				DB::delete("filter",		"fgid=$fgroup_del");
			}
			
			AZLib::getFilters(1);
			
			Url::redirect_current(array('cmd'));
		}
		#Xóa filter_group
		#############################################################################
	}
	
	function on_submit()
	{
		$gfsubmit = Url::get("gfsubmit");
		
		if($gfsubmit)//Cập nhật thuộc tính
		{
			$fgroup_id = (int)implode(array_keys($gfsubmit));
			if($fgroup_id)
			{
				$fgroup = DB::select("filter_group","id=$fgroup_id");
				
				if($fgroup)
				{
					$gf_name 	= Url::get('gf_name');
					$gf_pos 	= Url::get('gf_pos');
					
					if(isset($gf_name[$fgroup_id],$gf_pos[$fgroup_id]) && $gf_name[$fgroup_id] && $gf_pos[$fgroup_id])
					{
						$gf_name 	= $gf_name[$fgroup_id];
						$gf_pos 	= $gf_pos[$fgroup_id];
					
						if($fgroup['name'] != $gf_name || $fgroup['pos'] != $gf_pos)//Đổi tên, vị trí
						{
							$max_pos	=	(int)DB::fetch("SELECT max(pos) AS max_pos FROM filter_group",'max_pos',0);
				
							if($gf_pos > $max_pos)
							{
								$gf_pos = $max_pos;
							}
							elseif($gf_pos<=0)
							{
								$gf_pos = $fgroup['pos'];
							}
						
							DB::update('filter_group', array("name" => $gf_name, "pos" => $gf_pos) , "id=$fgroup_id");
					
							if(DB::select('filter_group',"pos=$gf_pos AND id!=$fgroup_id"))
							{
								DB::query("UPDATE filter_group SET pos=pos+1 WHERE pos>=$gf_pos AND pos<={$fgroup['pos']} AND id!=$fgroup_id");
							}
							
							AZLib::getFilters(1);
						}
					}
				}
			}
			
			Url::redirect_current(array('cmd'));
		}
		
		$submit = Url::get("submit");
		
		if($submit == "Thêm nhóm thuộc tính")
		{
			$gfilter_name	= Url::get('gfilter_name');
			$gfilter_pos	= Url::get('gfilter_pos');
			
			if($gfilter_name!='' && $gfilter_pos)
			{
				$gfilter_group =	array(	
											'name'		=>$gfilter_name,
											'catids'	=>$this->cat['id'],
											'pos'		=>$gfilter_pos
										 ); 

				$max_pos	=	(int)DB::fetch("SELECT max(pos) AS max_pos FROM filter_group",'max_pos',0);
			
				if($gfilter_pos	>	$max_pos || $gfilter_pos<=0)
				{
					$gfilter_pos	=	$max_pos+1;
				}
				
				$gfilter_group['pos'] = $gfilter_pos;
				
				$id = DB::insert('filter_group', $gfilter_group);
				
				if($id && DB::select('filter_group','pos="'.$gfilter_pos.'" AND id!='.$id)){
					DB::query('UPDATE filter_group SET pos=pos+1 WHERE pos>='.$gfilter_pos.' AND id!='.$id);
				}
			}
			
			AZLib::getFilters(1);
			
			Url::redirect_current(array('cmd'));
		}
		
		Url::redirect_current(array('cmd'));
	}
	
	function draw()
	{	
		global $display;
		$this->beginForm();
		
		$filter_groups 			= array();
		
		$max_pos_gfilter = 1;

		if(CGlobal::$all_gfilters)
		{
			foreach (CGlobal::$all_gfilters as $filter_group)
			{
				if($max_pos_gfilter <= $filter_group['pos'])
				{
					$max_pos_gfilter = $filter_group['pos'] + 1;
				}
				
				$filter_group['link'] 	= Url::build_current(array('cmd'=>'filter','fgroup_id'=>$filter_group['id']));
				
				$filter_group['del'] 	= Url::build_current(array('cmd','fgroup_del'=>$filter_group['id']));
				
				$filter_groups[$filter_group['id']] = $filter_group;
			}
		}
		
		$display->add('filter_groups',$filter_groups);
		
		$display->add('option_gfilter_pos',AZLib::getOptionNum(1,$max_pos_gfilter,$max_pos_gfilter));
		
		$display->output('FilterList');
		$this->endForm();
	}
}
?>